/**
 * 
 */
package com.wolfpire.info.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wolfpire.info.dao.TeachersCountDetailInfoDao;
import com.wolfpire.info.dao.TeachersCountInfoDao;
import com.wolfpire.info.model.TeachersCountDetailInfo;
import com.wolfpire.info.model.TeachersCountInfo;
import com.wolfpire.info.service.TeachersCountInfoService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;

/**
 * @author lihd
 *
 */

@Service("teachersCountInfoService")
public class TeachersCountInfoServiceImpl extends
		BaseHibernateService<TeachersCountInfo, Long> implements
		TeachersCountInfoService {

	@Override
	public List<TeachersCountInfo> list(TeachersCountInfo teachersCountInfo) {
		return this.teachersCountInfoDao.list(teachersCountInfo);
	}

	@Override
	public void del(Long id) {
		TeachersCountInfo teachersCountInfo = this.get(id);
		if (null != teachersCountInfo) {
			teachersCountInfo.setDelFlag(Constants.DELETE_FLAG);
			this.teachersCountInfoDao.save(teachersCountInfo);
		}
	}

	@Override
	public Page<TeachersCountInfo> setPageDataList(
			Page<TeachersCountInfo> page,
			TeachersCountInfo filterTeachersCountInfo) {
		return this.teachersCountInfoDao.findTeachersCountInfos(page, filterTeachersCountInfo);
	}

	@Override
	public TeachersCountInfo getFullTeachersCountInfo(Long id) {
		// TODO Auto-generated method stub
		TeachersCountInfo teachersCountInfo = this.teachersCountInfoDao.get(id);
		TeachersCountDetailInfo filter = new TeachersCountDetailInfo();
		filter.setTeachersCountInfoId(id);
		List<TeachersCountDetailInfo> details = this.teachersCountDetailInfoDao.list(filter);
		for (TeachersCountDetailInfo detail : details) {
			if ("full_time".equals(detail.getTeacherKey())) {
				// 专任教师
				teachersCountInfo.setFullTimeDetail(detail);
			} else if ("part_time".equals(detail.getTeacherKey())) {
				teachersCountInfo.setPartTimeDetail(detail);
			}
		}
		return teachersCountInfo;
	}

	@Override
	public void saveOrUpdate(TeachersCountInfo teachersCountInfo) {
		this.teachersCountInfoDao.saveOrUpdate(teachersCountInfo);
		List<TeachersCountDetailInfo> details = new ArrayList<TeachersCountDetailInfo>();
		details.add(teachersCountInfo.getFullTimeDetail());
		details.add(teachersCountInfo.getPartTimeDetail());
		Long id = teachersCountInfo.getId();
		// 删除子表关联记录，然后重新插入
		this.teachersCountDetailInfoDao.deleteByTeachersCountInfoId(id);
		for (TeachersCountDetailInfo detail : details) {
			detail.setTeachersCountInfoId(id);
		}
		this.teachersCountDetailInfoDao.batchSave(details);
	}

	@Override
	protected IBaseHibernateDao<TeachersCountInfo, Long> getEntityDao() {
		return this.teachersCountInfoDao;
	}
	
	@Resource private TeachersCountInfoDao teachersCountInfoDao;
	
	@Resource private TeachersCountDetailInfoDao teachersCountDetailInfoDao;
}
